﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Collections.Generic;
using System.Text;

namespace Hisun.View.Meterial
{
    public partial class InsertDirectMeterial : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();

        public string alert;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //将从发送页的当前作业中心ID,中间体ID保存到本页的隐藏字段

            
                HF_CenterId.Value = Request.QueryString["centerid"].ToString();
           
                HF_PIPId.Value = Request.QueryString["PIPid"].ToString();
                int myCenterId = Convert.ToInt32(HF_CenterId.Value);
                //查询出当前作业中心ID对应的作业中心名称
                var myquery = (from a in db.TABC_JS_CostCenter where a.作业中心ID == myCenterId select a).Single();
                TextBox_CenterName.Text = myquery.作业中心名称;
                var myquery2 = (from b in db.TABC_JS_ProductInProcess
                                where b.作业中心ID == myCenterId
                                     && b.中间体ID == HF_PIPId.Value
                                select b).Single();

                TextBox_PIPName.Text = myquery2.中间体名称;


                TextBox_QiChuJinE.Text = "0";
                TextBox_QiChuShuLiang.Text = "0";
                TextBox_LingYongJinE.Text = "0";
                TextBox_LingYongShuLiang.Text = "0";
                TextBox_BenQiHaoYong.Text = "0";
                TextBox_HaoYongJinE.Text = "0";
                TextBox_QiMoJinE.Text = "0";
                TextBox_QiMoShuLiang.Text = "0";

            }
        }


        //填充选择的材料ID
        protected void Button_SelectMeterialId_Click(object sender, EventArgs e)
        {
            StringBuilder s = new StringBuilder();
            s.Append("<script language=javascript>");
            s.Append("var a=window.showModalDialog('InsertDirectMeterial_SelectMeterialId.aspx');");
            s.Append("if(a!=null)");
            s.Append(@"{ var aa =a.split(""&"") ;");
            s.Append("document.all('TextBox_MeterialId').value=aa[0];");
            s.Append("document.all('TextBox_MeterialName').value=aa[1];");
            s.Append("document.all('TextBox_Unit').value=aa[2];");
            s.Append("document.all('MyMeterialInfo').value=a;}");
            s.Append("</script>");
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            string sname = "lt";
            if (!cs.IsStartupScriptRegistered(cstype, sname))
            { cs.RegisterStartupScript(cstype, sname, s.ToString()); }
        }

        //提交当前的数据
        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string[] myarray_MeterialInfo = MyMeterialInfo.Value.Split('&');
            string MeterialId_Save = myarray_MeterialInfo[0];
            string MeterialUnit_Save = myarray_MeterialInfo[2];

            bool IsProduct = true;

            if( myarray_MeterialInfo[3]=="0")
            {
                 IsProduct = false ;
            }


            var isExistRow1 = (from m in db.TABC_JS_ProductInProcess
                              where 
                               m.作业中心ID == Convert.ToInt32(HF_CenterId.Value)
                               && m.中间体ID == HF_PIPId.Value
                              select m).Count();



            if (isExistRow1 == 0)
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('当前作业中心或中间体ID不可用'); </script>");
                return; 
                //Response.End();

            }

            var isExistRow2 = (from m in db.TABC_JC_MeterialResumption
                              where m.会计期间ID == Application[ServerVars.sysApplcationAccountPeriod].ToString()
                              &&  m.材料ID == MeterialId_Save
                              && m.作业中心ID == Convert.ToInt32(HF_CenterId.Value)
                              && m.中间体ID == HF_PIPId.Value
                              select m).Count();

            if (isExistRow2 != 0)
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('当前材料记录已存在'); </script>");
                return; 

            }

          //  decimal isRigthShuLiang = Convert.ToDecimal(TextBox_QiChuShuLiang.Text)+Convert.ToDecimal(TextBox_LingYongShuLiang.Text)-Convert.ToDecimal(TextBox_QiMoShuLiang.Text);



            TABC_JC_MeterialResumption myNewRow = new TABC_JC_MeterialResumption();
            myNewRow.公司ID = "hisun";
            myNewRow.账套ID = "001";
            myNewRow.会计期间ID = Application[ServerVars.sysApplcationAccountPeriod].ToString();
            myNewRow.作业中心ID = Convert.ToInt32(HF_CenterId.Value);
            myNewRow.中间体ID = HF_PIPId.Value;
            myNewRow.材料ID = MeterialId_Save;
            myNewRow.计量单位 = MeterialUnit_Save;
        //    myNewRow.期初数量 = Convert.ToDecimal(TextBox_QiChuShuLiang.Text);
         //   myNewRow.期初金额 = Convert.ToDecimal(TextBox_QiChuJinE.Text);
            myNewRow.领用数量 = Convert.ToDecimal(TextBox_LingYongShuLiang.Text);
            myNewRow.领用金额 = Convert.ToDecimal(TextBox_LingYongJinE.Text);
       //     myNewRow.本期耗用 = isRigthShuLiang;
       //     myNewRow.耗用金额 = isRigthJinE;
            myNewRow.期末数量 = Convert.ToDecimal(TextBox_QiMoShuLiang.Text);
        //    myNewRow.期末金额 = Convert.ToDecimal(TextBox_QiMoJinE.Text);
            myNewRow.产品领料 = IsProduct;
            myNewRow.手工调整 = true;
            

            db.TABC_JC_MeterialResumption.InsertOnSubmit(myNewRow);

            try
            {
                db.SubmitChanges();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('保存成功');history.go(-3) ;</script>");
              
            }
            catch 
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('保存失败，请与管理员联系'); </script>");
              
            }




        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/View/Meterial/DirectMeterialModify.aspx");
        }




    }
}
